﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using MathematicallySafe.HeadCounter.Data.Common;
using MathematicallySafe.HeadCounter.Data;
using MathematicallySafe.HeadCounter.Data.Interfaces;
using StructureMap;

namespace MathematicallySafe.HeadCounter.Services.Exporters
{
    public class HtmlExport
    {
        private static readonly IGuild Guild = ObjectFactory.GetInstance<IGuild>();

        public void Export()
        {
            PageController pageController = CreatePages();
            var htmlGenerator = new HtmlPageGenerator(Guild.Name, pageController);

            pageController.ForEach(htmlGenerator.CreatePage);
        }

        private static PageController CreatePages()
        {
            var pages = new PageController
                        {
                            {"att6.html", "6Wk Attendance", GenerateShortAttendanceContent},
                            {"att12.html", "12Wk Attendance", GenerateLongAttendanceContent},
                            {"priority.html", "Loot Priority", GeneratePriorityContent}
                        };

            return pages;
        }

        private static XElement GenerateShortAttendanceContent()
        {
            XElement content = HtmlSnippetHelper.CenteredContentContainer(22, 
                               new XElement("table",
                                            new XElement("caption", "Raiders (By 6wkAtt%)"),
                                            new XElement("tr",
                                                         new XElement("th", "Name"),
                                                         new XElement("th", "Class"),
                                                         new XElement("th", "6WkAtt%"),
                                                         new XElement("th", "Count"),
                                                         new XElement("th", "Value"),
                                                         new XElement("th", "Rate"),
                                                         new XElement("th", "Priority")
                                                ),
                                            from r in Guild.ActiveRaidersByMain
                                            orderby r.ShortPeriodAttendance descending 
                                            select new XElement("tr",
                                                                new XElement("td", HtmlSnippetHelper.MakeRaiderCell(r.MainName)),
                                                                new XElement("td", r.Class),
                                                                new XElement("td", r.ShortPeriodAttendance.ToString("F2") + " / " + r.ShortPeriodAllAttendance.ToString("F2")),
                                                                new XElement("td", r.ShortPeriodLootCount),
                                                                new XElement("td", r.ShortPeriodLootValue.ToString("F2")),
                                                                new XElement("td", r.ShortPeriodLootRate.ToString("F2")),
                                                                new XElement("td", r.ShortPeriodRelativeLootPriorty.HasValue ? r.ShortPeriodRelativeLootPriorty.Value.ToString("F2") : "--")
                                                )
                                   )
                );

            return content;
        }

        private static XElement GenerateLongAttendanceContent()
        {
            XElement content = HtmlSnippetHelper.CenteredContentContainer(22, 
                               new XElement("table",
                                            new XElement("caption", "Raiders (By 12wkAtt%)"),
                                            new XElement("tr",
                                                         new XElement("th", "Name"),
                                                         new XElement("th", "Class"),
                                                         new XElement("th", "12WkAtt%"),
                                                         new XElement("th", "Count"),
                                                         new XElement("th", "Value"),
                                                         new XElement("th", "Rate"),
                                                         new XElement("th", "Priority")
                                                ),
                                            from r in Guild.ActiveRaidersByMain
                                            orderby r.LongPeriodAttendance descending 
                                            select new XElement("tr",
                                                                new XElement("td", HtmlSnippetHelper.MakeRaiderCell(r.MainName)),
                                                                new XElement("td", r.Class),
                                                                new XElement("td", r.LongPeriodAttendance.ToString("F2") + " / " + r.LongPeriodAllAttendance.ToString("F2")),
                                                                new XElement("td", r.LongPeriodLootCount),
                                                                new XElement("td", r.LongPeriodLootValue.ToString("F2")),
                                                                new XElement("td", r.LongPeriodLootRate.ToString("F2")),
                                                                new XElement("td", r.LongPeriodRelativeLootPriorty.HasValue ? r.LongPeriodRelativeLootPriorty.Value.ToString("F2") : "--")
                                                )
                                   )
                );

            return content;
        }

        private static XElement GeneratePriorityContent()
        {
            XElement content = HtmlSnippetHelper.CenteredContentContainer(22, 
                               new XElement("table",
                                            new XElement("caption", "Raiders (By Priority)"),
                                            new XElement("tr",
                                                         new XElement("th", "Name"),
                                                         new XElement("th", "Class"),
                                                         new XElement("th", "6WkAtt%"),
                                                         new XElement("th", "Count"),
                                                         new XElement("th", "Value"),
                                                         new XElement("th", "Rate"),
                                                         new XElement("th", "Priority")
                                                ),
                                            from r in Guild.ActiveRaidersByMain
                                            orderby r.ShortPeriodLootPriority, r.MainName ascending
                                            select new XElement("tr",
                                                                new XElement("td", HtmlSnippetHelper.MakeRaiderCell(r.MainName)),
                                                                new XElement("td", r.Class),
                                                                new XElement("td", r.ShortPeriodAttendance.ToString("F2") + " / " + r.ShortPeriodAllAttendance.ToString("F2")),
                                                                new XElement("td", r.ShortPeriodLootCount),
                                                                new XElement("td", r.ShortPeriodLootValue.ToString("F2")),
                                                                new XElement("td", r.ShortPeriodLootRate.ToString("F2")),
                                                                new XElement("td", r.ShortPeriodRelativeLootPriorty.HasValue ? r.ShortPeriodRelativeLootPriorty.Value.ToString("F2") : "--")

                                                )
                                   )
            );

            return content;
        }
    }
}